#!/usr/bin/python
import unittest
from StringIO import StringIO
import sys
import os

from uya.routerconf.configmod import misc
from uya.routerconf.configmod import config as configmod

expected_config = {
    "section1": {
        "string": "myhostname",
        "index": 1,
    },
    "section2": {
        "key1": "value1",
        "subsection21": {
            "key21": "value21",
        }, 
    }
}

class TestConfig(unittest.TestCase):        
    def load_config(self, basefile):
        tests_dir = os.path.dirname(sys.argv[0])
        conf_dir = os.path.join(tests_dir, "fixtures")
        config_file = os.path.join(conf_dir, "%s.conf" % basefile)
        template_file = os.path.join(conf_dir, "%s.template.conf"  % basefile)
        return configmod.load_config(config_file, template_file)

    def test_load_config(self):
        config = self.load_config("example")
        self.assertEqual(expected_config, dict(config))                 

    def test_load_config_with_errors(self):
        misc.verbose_level = -1 
        self.assertRaises(configmod.ConfigError, 
            self.load_config, "example-error")                 
         
def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TestConfig)
                 
if __name__ == '__main__':
    unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
